//
//  ShaiorderViewController.m
//  danpin
//
//  Created by chuangjia on 2/9/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "ShaiorderViewController.h"
#import "ShaiorderDetailViewController.h"
#import "CellConfig.h"
#import "ZShare.h"
#import "CommentViewController.h"
#import "SearchViewController.h"
#import "BaseModel.h"
#import "UserLoginVC.h"
#import "ZToastManager.h"
#import "UserRegisterNameVC.h"
#import "UserNavigationController.h"

#import "RootViewController.h"
#import "OtherUserCenterVC.h"
#import "GoodConfig.h"
#import "ShaiDanVC.h"
#import "LjjUISegmentedControl.h"
#import "WKAlertView.h"
#import "WebViewController.h"
@interface ShaiorderViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,UserDelegate,LjjUISegmentedControlDelegate,UIActionSheetDelegate>
{
    CGFloat _oldOffset;//位移
}
@property(nonatomic,strong)NSMutableArray *datasArray;
@property(nonatomic,strong)NSString *curAction;
@property(nonatomic,strong)NSMutableArray *tagsArray;
@property(nonatomic,strong)UITableView *tbView;
@property(nonatomic,strong)NSString *currentTag;
@property(nonatomic,assign)BOOL isLoadFinished;
@property(nonatomic,assign)BOOL isRequesting;
@property(nonatomic,assign)int currentPage;
@property(nonatomic,assign)BOOL isRefresh;
@property (strong, nonatomic) LjjUISegmentedControl* pageController;

@end

@implementation ShaiorderViewController
@synthesize pageController=_pageController;
- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    self.view.backgroundColor =[UIColor whiteColor];
    [UMengShare event:CK_DISCOVERY acc:1];
    if (!_datasArray) {
        _datasArray =[[NSMutableArray alloc] init];
    }
    if (!_tagsArray) {
        _tagsArray =[[NSMutableArray alloc] init];
    }
    self.isLoadFinished =NO;
    self.isRequesting = NO;
    self.currentTag=@"";
    self.currentPage = 0;
 
    //
    self.isRefresh = YES;
    
    _tagsArray =[NSMutableArray arrayWithArray:[ZShare sharedInstance].IndexTagArray];
    if (_tagsArray.count>0) {
        NSDictionary *dict =[_tagsArray objectAtIndex:0];
        self.curAction =[NSString stringWithFormat:@"%@",[dict objectForKey:@"action"]];
    }
    else{
        self.curAction =@"shaidan";
    }
    
    [self initNavView];
    [self notificationManager];
    [self initUI];
    
//    [ZShare showAnimationView:self.view center:CGPointZero];
    [self reloadData];
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    [[SDImageCache sharedImageCache] clearMemory];
    DLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)reloadData
{
    self.isRefresh = YES;
    self.currentPage=0;
    [self.tbView setShowsInfiniteScrolling:YES];
    [self getCategoryData:self.curAction tag:self.currentTag];
}
-(void)scrollToTop
{
    if(_tbView)
    {
        [_tbView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
    }
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.rootViewController.tabbar setHidden:NO];
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.rootViewController.tabbar setHidden:YES];
}
-(void)dealloc
{
    if (_pageController) {
        _pageController.delegate=nil;
        [_pageController removeFromSuperview];
        _pageController =nil;
    }
    DLog(@"%s", __PRETTY_FUNCTION__);
}


-(void)initNavView
{
    UIView *headerView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, hightAuto(65))];
    headerView.backgroundColor=[UIColor clearColor];
    UIImageView *backView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, hightAuto(65))];
    backView.image =[UIImage imageNamed:@"titile_bg.png"];
    [headerView addSubview:backView];
    backView=nil;
    
    if (_pageController) {
         _pageController.delegate=nil;
        [_pageController removeFromSuperview];
        _pageController =nil;
    }
    _pageController =[[LjjUISegmentedControl alloc] initWithFrame:CGRectMake((DEVICE_WIDTH-PHOTO_FRAME_WIDTH*18)/2, (hightAuto(65)-20)/2, PHOTO_FRAME_WIDTH*18, 20)];
    _pageController.backgroundColor = [UIColor clearColor];
    _pageController.delegate=self;
    [headerView addSubview:_pageController];
    
    if (_tagsArray.count>0) {
        NSMutableArray *titleArray =[NSMutableArray array];
        for (id tempD in _tagsArray) {
            if (tempD && [tempD isKindOfClass:[NSDictionary class]]) {
                NSString *title =[NSString stringWithFormat:@"%@",[tempD objectForKey:@"title"]];
                [titleArray addObject:title];
            }
        }
        [_pageController AddSegumentArray:titleArray];
    }else{
       [_pageController AddSegumentArray:@[@"最新",@"关注"]];
    }
    
    //喜欢
    UIButton *shaidanBtn= [UIButton buttonWithType:UIButtonTypeCustom];
    shaidanBtn.frame = CGRectMake(DEVICE_WIDTH -24- PHOTO_FRAME_WIDTH*2,18+(hightAuto(65)-24-20)/2, 24, 24);
    [shaidanBtn setImage:[UIImage imageNamed:@"title_search_ico.png"] forState:UIControlStateNormal];
    [shaidanBtn addTarget:self action:@selector(clickNavEvent) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:shaidanBtn];
    
    [self.view addSubview:headerView];
    headerView=nil;
}
-(void)clickNavEvent
{
    SearchViewController *search =[[SearchViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
}
-(void)uisegumentSelectionChange:(NSInteger)selection
{
    if (_tagsArray.count>0)
    {
        if (selection>_tagsArray.count) {
            return;
        }
        NSDictionary *dict =[_tagsArray objectAtIndex:selection];
        self.curAction =[NSString stringWithFormat:@"%@",[dict objectForKey:@"action"]];
        
    }else{
        if (selection==0) {
            self.curAction =@"shaidan";
        }else{
            self.curAction =@"follow";
            
        }
    }
    
    self.currentPage=0;
    [self.datasArray removeAllObjects];
    [self.tbView reloadData];
    [self reloadData];
}
-(void)initUI
{
    _tbView = [[UITableView alloc] initWithFrame:CGRectMake(0, hightAuto(65), self.view.frame.size.width, self.view.frame.size.height-hightAuto(65)-hightAuto(45)) style:UITableViewStyleGrouped];
    _tbView.delegate = self;
    _tbView.dataSource = self;
    _tbView.backgroundColor = [UIColor clearColor];
    _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tbView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tbView];
    
    self.currentPage = 0;
    
    __weak typeof (self) wSelf = self;
    
    [_tbView addPullToRefreshWithActionHandler:^{
        wSelf.isRequesting = YES;
        wSelf.isRefresh = NO;
        wSelf.currentPage = 0;
        [wSelf.tbView.pullToRefreshView startAnimating];
        [wSelf getCategoryData:wSelf.curAction tag:wSelf.currentTag];
    }];
    [_tbView.pullToRefreshView setOYStyle];
    // setup infinite scrolling
    [self.tbView addInfiniteScrollingWithActionHandler:^{
        if (wSelf.isRequesting) {
            return ;
        }
        wSelf.isRefresh = NO;
        if (wSelf.isLoadFinished) {
            //停止菊花
            [wSelf.tbView.infiniteScrollingView stopAnimating];
            [wSelf.tbView setShowsInfiniteScrolling:NO];
            return;
        }
        [wSelf.tbView.infiniteScrollingView startAnimating];
        wSelf.currentPage =(int) [wSelf.datasArray count];
        [wSelf getCategoryData:wSelf.curAction tag:wSelf.currentTag];
        
    }];
    
    //
}
#pragma mark - tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  [_datasArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *data =[_datasArray objectAtIndex:indexPath.row];
    return [ShaiorderCell returnCellHeight:data];
    
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *data =[_datasArray objectAtIndex:indexPath.row];
    ShaiorderCell *cell = (ShaiorderCell*)[tableView dequeueReusableCellWithIdentifier:@""]; //出列可重用的cell
    if(cell == nil)
    {
        cell = [[ShaiorderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.backgroundColor=[UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak typeof (self) wSelf = self;
        [cell setCallBack:^(BEventType t, UIView *v,id obj ,id key,NSIndexPath *indexPath) {
            [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
        }];
    }
    if (indexPath.row>=[_datasArray count]) {
        return cell;
    }
    [cell showInfo:data other:[NSNumber numberWithBool:YES] key:@"ShaiorderCell" indexPath:indexPath];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *data =[_datasArray objectAtIndex:indexPath.row];
    NSString *is_shaidan = [data objectForKey:@"is_shaidan"];
    if([is_shaidan intValue]==20)
    {
        WebViewController *wev =[[WebViewController alloc] init];
        wev.titlestr=@"shaidan";
        NSString *urltr =[data objectForKey:@"url"];
        wev.jsonUrl =urltr;
        [self.navigationController pushViewController:wev animated:YES];
        wev=nil;
    }else{
        ShaiorderDetailViewController *good =[[ShaiorderDetailViewController alloc] init];
        good.type =-1;
        good.dictionary = data;
        good.photo_id = [NSString stringWithFormat:@"%@",[data objectForKey:@"photo_id"]];
        [self.navigationController pushViewController:good animated:YES];
    }
 
}

-(void)cellAction:(BEventType)type views:(UIView *)v obj:(id)obj key:(id)key indexPath:(NSIndexPath *)indexPath
{
    //    DLog(@"----cellAction--------%d-%@",type,v);
    switch (type) {
        case BEventType_search:
        {
            SearchViewController *search =[[SearchViewController alloc] init];
            [self.navigationController pushViewController:search animated:YES];
        }
            break;
        case BEventType_category:
        {
            GoodClassViewController *cate =[[GoodClassViewController alloc] init];
            cate.cateCid =[NSString stringWithFormat:@"%@",[obj objectForKey:@"cat2_id"]];
            cate.cateName =[NSString stringWithFormat:@"%@",[obj objectForKey:@"cat2"]];
            cate.cateTag =@"";
            [self.navigationController pushViewController:cate animated:YES];
        }
            break;
        case BEventType_categoryTag:
        {
            GoodClassViewController *cate =[[GoodClassViewController alloc] init];
            cate.cateCid =[NSString stringWithFormat:@"%@",[obj objectForKey:@"cat2_id"]];
            cate.cateName =[NSString stringWithFormat:@"%@",[obj objectForKey:@"cat2"]];
            cate.cateTag =[NSString stringWithFormat:@"%@",key];
            [self.navigationController pushViewController:cate animated:YES];
        }
            break;
        case BEventType_brand:
        {
            GoodBrandViewController *brand =[[GoodBrandViewController alloc] init];
            brand.cId =  [NSString stringWithFormat:@"%@",[obj objectForKey:@"cat2_id"]];
            brand.brandName =[NSString stringWithFormat:@"%@",[obj objectForKey:@"brand"]];
            brand.brandId =[NSString stringWithFormat:@"%@",[obj objectForKey:@"brand_id"]];
            [self.navigationController pushViewController:brand animated:YES];
        }
            break;
        case BEventType_good:
        {
            
            ShaiorderDetailViewController *good =[[ShaiorderDetailViewController alloc] init];
            good.type =-1;
            good.dictionary =obj;
            good.photo_id = [NSString stringWithFormat:@"%@",[obj objectForKey:@"photo_id"]];
            [self.navigationController pushViewController:good animated:YES];
        }
            break;
        case BEventType_goodforsun:
        {
            ShaiorderDetailViewController *good =[[ShaiorderDetailViewController alloc] init];
            good.type =-1;
            good.dictionary =obj;
            good.photo_id = [NSString stringWithFormat:@"%@",[obj objectForKey:@"good_id"]];
            [self.navigationController pushViewController:good animated:YES];
        }
            break;
        case BEventType_comment:
        {
            NSString *pid =[NSString stringWithFormat:@"%@",[obj objectForKey:@"photo_id"]];
            CommentViewController *comm =[[CommentViewController alloc] init];
            comm.photo_id =pid;
            comm.dictionary =obj;
            comm.fromType=@"1";
            [self.navigationController pushViewController:comm animated:YES];
        }
            break;
        case BEventType_like:
        {
            NSString *pid =[NSString stringWithFormat:@"%@",[obj objectForKey:@"photo_id"]];
            if ([self isLogin]) {
                [self likeWithPid:pid indexPath:indexPath];
            }
        }
            break;
        case BEventType_sunUser:
        {
            OtherUserCenterVC *userf =[[OtherUserCenterVC alloc] init];
            NSMutableDictionary *dict =[NSMutableDictionary dictionary];
            NSDictionary *user = [obj objectForKey:@"user"];
            NSString*largeFace =  [[NSString stringWithFormat:@"%@",[user objectForKey:@"largeFace"]] replaceNullString];
            NSString*user_id =  [[NSString stringWithFormat:@"%@",[user objectForKey:@"user_id"]] replaceNullString];
            NSString*nick_name =  [[NSString stringWithFormat:@"%@",[user objectForKey:@"nick_name"]] replaceNullString];
            
            [dict setValue:largeFace forKey:@"largeFace"];
            [dict setValue:user_id forKey:@"user_id"];
            [dict setValue:nick_name forKey:@"nick_name"];
            
            userf.userDictionary=dict;
            [self.navigationController pushViewController:userf animated:YES];
            
        }break;
        case BEventType_userfollow:
        {
            if ([self isLogin]) {
                //            __weak typeof (self) wSelf = self;
                NSMutableDictionary *dict =[NSMutableDictionary dictionaryWithDictionary:obj];
                NSDictionary *user =[dict objectForKey:@"user"];
                NSString *user_id = [[NSString stringWithFormat:@"%@",[user objectForKey:@"user_id"]] replaceNullString];
                NSString *nick_name = [[NSString stringWithFormat:@"%@",[user objectForKey:@"nick_name"]] replaceNullString];
                [dict setValue:@"follow_user" forKey:@"action"];
                [dict setValue:@"" forKey:@"brand_id"];
                [dict setValue:@"" forKey:@"cate_id"];
                [dict setValue:user_id forKey:@"follow_id"];
                [dict setValue:nick_name forKey:@"follow_name"];
                
                [dict setValue:@"" forKey:@"tag"];
                __weak typeof (self) wSelf = self;
                [BaseModel follow:dict success:^(AFHTTPRequestOperation* operation, id result){
                    
                    if (result&&[result isKindOfClass:[NSDictionary class]]) {
                        NSDictionary *resultDist =(NSDictionary*)result;
                        [[ZToastManager ShardInstance] showtoast:[resultDist objectForKey:@"msg"]];
                        if ([wSelf.curAction isEqualToString:@"follow"])
                        {
                            for (int i=0; i<[wSelf.datasArray count]; i++) {
                                NSDictionary *dict =[wSelf.datasArray objectAtIndex:i];
                                NSDictionary *user = [dict objectForKey:@"user"];
                                NSString *pidd = [[NSString stringWithFormat:@"%@",[user objectForKey:@"user_id"]] replaceNullString];
                                if ([pidd isEqualToString:user_id])
                                {
                                    [wSelf.datasArray removeObjectAtIndex:i];
                                }
                            }
                            [wSelf.tbView reloadData];
                        }else
                        {
                            for (int i=0; i<[wSelf.datasArray count]; i++) {
                                NSDictionary *dict =[wSelf.datasArray objectAtIndex:i];
                                if ([dict isKindOfClass:[NSDictionary class]]) {
                                    NSMutableDictionary *temp = [dict mutableCopy];
                                    NSMutableDictionary *user = [[temp objectForKey:@"user"] mutableCopy];
                                    NSString *pidd = [[NSString stringWithFormat:@"%@",[user objectForKey:@"user_id"]] replaceNullString];
                                    if ([pidd isEqualToString:user_id]) {
                                            [user setValue:[[NSString stringWithFormat:@"%@",[result objectForKey:@"followed"]] replaceNullString] forKey:@"followed"];
                                            [temp setObject:user forKey:@"user"];
                                            user =nil;
                                            [wSelf.datasArray replaceObjectAtIndex:i withObject:temp];
                                            temp=nil;
                                    }
                                }
                            }
                            
                        }
                        
                        [_tbView reloadData];
                    }
                    
                } failure:^(NSError* error){
                    [[ZToastManager ShardInstance] showtoast:@"关注失败"];
                }];
            }
            break;
            case BEventType_more:
            {
                if ([self isLogin]) {
                    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                                  initWithTitle:nil
                                                  delegate:self
                                                  cancelButtonTitle:@"取消"
                                                  destructiveButtonTitle:nil
                                                  otherButtonTitles:@"不感兴趣",nil];
                    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
                    [actionSheet showInView:self.view];
                }
            }
            break;
        }
            break;
        default:
            break;
    }
    
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex)
    {
        case 0:  //修改图片
          
            break;
        case 1:  //删除图片
            
            break;
    }
}
#pragma mark -----
-(void)getCategoryData:(NSString*)action tag:(NSString*)tag
{
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:action forKey:@"action"];
    [dict setValue:@"" forKey:@"guest_id"];
    [dict setValue:@"" forKey:@"user_id"];
    [dict setValue:@"" forKey:@"user_name"];
    [dict setValue:@"" forKey:@"cate_id"];
    [dict setValue:@"" forKey:@"brand_id"];
    [dict setValue:@"" forKey:@"sku_name"];
    [dict setValue:tag forKey:@"tag"];
    [dict setValue:[NSString stringWithFormat:@"%d",self.currentPage] forKey:@"from"];
    [dict setValue:@"1" forKey:@"grid"];
    [dict setValue:@"" forKey:@"lat"];
    [dict setValue:@"" forKey:@"lon"];
    self.isRequesting =YES;
    __weak typeof (self) wSelf = self;
    wSelf.isRequesting = YES;
    [BaseModel wareList:dict  allowFetchCache:self.isRefresh  success:^(AFHTTPRequestOperation *operation, NSObject *result) {
        [wSelf parserResultObject:result];
        wSelf.isRequesting = NO;
        wSelf.isRefresh =NO;
        [_tbView.pullToRefreshView stopAnimating];
        [wSelf.tbView.infiniteScrollingView stopAnimating];
    //    [ZShare hiddenAnimationView:wSelf.view];
    } failure:^(NSError *error) {
        [_tbView.pullToRefreshView stopAnimating] ;
        [wSelf.tbView.infiniteScrollingView stopAnimating];
        wSelf.isRequesting = NO;
        wSelf.isRefresh =NO;
  //      [ZShare hiddenAnimationView:wSelf.view];
  //      [ZShare hiddenAnimationView:wSelf.view text:@""];
        
    }];
    
}
-(void)parserResultObject:(id)resultObject
{
    //    DLog(@"%@--",resultObject);
    if (resultObject==nil) {
        return;
    }
    if (resultObject && [resultObject isKindOfClass:[NSDictionary class]]) {
        
        
        id dataarray =[resultObject objectForKey:@"data"];
        if (dataarray && [dataarray isKindOfClass:[NSArray class]]) {
            if (self.currentPage == 0 && [dataarray count]>0) {
                [_datasArray removeAllObjects];
            }
            for (id di in dataarray) {
                if (di && [di isKindOfClass:[NSDictionary class]])
                {
                    [_datasArray addObject:di];
                }
            }
            self.tbView.tableFooterView=nil;
        }
        self.isLoadFinished =  [[resultObject objectForKey:@"end"] boolValue];
        if (self.isLoadFinished) {
            
            NSString *end_msg =[resultObject objectForKey:@"end_msg"];
            self.tbView.tableFooterView =[ZShare footerView:end_msg];
            
            [self.tbView.infiniteScrollingView stopAnimating];
            [self.tbView setShowsInfiniteScrolling:NO];
        }
    }
    if (_datasArray.count==0 && [self.curAction isEqualToString:@"follow"]) {
        UITapGestureRecognizer* singleRecognizer= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFollow)];
        UIView *tempView =[[UIView alloc] initWithFrame:CGRectMake(0, 0,DEVICE_WIDTH,self.tbView.frame.size.height)];
        tempView.backgroundColor=[UIColor clearColor];
        tempView.userInteractionEnabled=YES;
        [tempView addGestureRecognizer:singleRecognizer];
        singleRecognizer=nil;
        UIImage *tmimage =[UIImage imageNamed:@"follow_default_image.png"];
        UIImageView *tempImageView =[[UIImageView alloc] initWithFrame:CGRectMake((tempView.frame.size.width-widthAuto(278))/2, (tempView.frame.size.height-hightAuto(423))/2-20, widthAuto(278), hightAuto(423))];
        tempImageView.image =tmimage;
        [tempView addSubview:tempImageView];
        tempImageView=nil;
        self.tbView.tableFooterView =tempView;
        tempView=nil;
    }
    //回调或者说是通知主线程刷新，
    [self.tbView reloadData];
    self.isRequesting = NO;
}
-(void)handleSingleTapFollow
{
     if ([self isLogin])
     {
         UserFollowViewController *follow =[[UserFollowViewController alloc] init];
         [self.navigationController pushViewController:follow animated:YES];
         follow=nil;
     }
}
//喜欢
-(void)likeWithPid:(NSString*)pid  indexPath:(NSIndexPath*)indexPath
{
    [UMengShare event:CK_LOVE acc:1];
    __weak typeof (self) wSelf = self;
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:pid forKey:@"photo_id"];
    [BaseModel love:dict success:^(AFHTTPRequestOperation *operation, id result) {
        int code =  [[result objectForKey:@"code"] intValue];
        NSString *like_cnt =  [NSString stringWithFormat:@"%@",[result objectForKey:@"like_cnt"]];
        if (code==1 || code==2) {
            for (int i=0; i<[wSelf.datasArray count]; i++) {
                NSDictionary *dict =[wSelf.datasArray objectAtIndex:i];
                if ([dict isKindOfClass:[NSDictionary class]]) {
                    NSString *pidd =[NSString stringWithFormat:@"%@",[dict objectForKey:@"photo_id"]];
                    if ([pidd isEqualToString:pid]) {
                        NSMutableDictionary *temp = [dict mutableCopy];
                        if (code==1) {
                            [temp setValue:@"1" forKey:@"like"];
                        }else{
                            [temp setValue:@"0" forKey:@"like"];
                        }
                        [temp setValue:like_cnt forKey:@"like_cnt"];
                        [wSelf.datasArray replaceObjectAtIndex:i withObject:temp];
                        break;
                    }
                }
            }
            [_tbView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
        }
        
    } failure:^(NSError *error) {
    //    [ZShare hiddenAnimationView:wSelf.view text:@""];
    }];
}
-(void)notificationManager
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationWillEnterForeground)
                                                 name:UIApplicationWillEnterForegroundNotification
                                               object:[UIApplication sharedApplication]];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateLoveList:)
                                                 name:@"updateLoveList"
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateLoveList:)
                                                 name:@"updateCommentList"
                                               object:nil];
    
    
}
-(void)updateLoveList:(NSNotification*)notification
{
    DLog(@"%@",notification.object);
    self.currentPage = 0;
    [self getCategoryData:self.curAction tag:self.currentTag];
}
-(void)applicationWillEnterForeground
{
    self.currentPage = 0;
    [self getCategoryData:self.curAction tag:self.currentTag];
}

-(void)clickNavEvent:(id)sender
{
    UIButton *btn =(UIButton *)sender;
    switch (btn.tag) {
        case 6:
        {
            ShaiDanVC *shaidanVC =[[ShaiDanVC alloc] init];
            shaidanVC.type =0;
            shaidanVC.rank_id =nil;
            shaidanVC.cateDictionary = nil;
            [self.navigationController pushViewController:shaidanVC animated:YES];
            shaidanVC=nil;
        }
            break;
        default:
            break;
    }
}

-(BOOL)isLogin
{
    if ([[ZShare sharedInstance] isLogin]) {
        return YES;
    }else
    {
        UserNavigationController *viewcontroller =[UserNavigationController newWithCameraDelegate:self];
        [self presentViewController:viewcontroller animated:NO completion:nil];
        
        return NO;
    }
    return NO;
}
-(void)userLoginOK:(id)userinfo
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)userDidCancel
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
